package widget.common.tools
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import widget.common.event.LoadDataEvent;

	public class ClassLoader extends DisplayLoader{
		public var uid:String = "";
		private var _loadinfo:LoaderInfo;
		public function ClassLoader()
		{
			super();
		}
		
		override protected function completeHandler(event : Event) : void
		{
			super.completeHandler(event);
			_loadinfo = event.currentTarget as LoaderInfo;
			this.dispatchEvent(event);
		}
		
		override public function loadByURL(url : String) : void
		{
			this.loadURL = url;
			
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			
			/** 加载到子域 */
			context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
			
			load(context);
		}
		
		override public function clear() : void
		{
			super.clear();
			_loadinfo = null;
		}
		
		/**
		 * 获取当前ApplicationDomain内的类定义
		 * 
		 * @param name 类名称，必须包含完整的命名空间,如 Grave.Function.SWFLoader
		 * @param info 加载swf的LoadInfo，不指定则从当前域获取
		 * @return 获取的类定义，如果不存在返回null
		 */
		public function getClassByName(name:String):Class 
		{
			try 
			{
				if (_loadinfo == null) 
				{
					return ApplicationDomain.currentDomain.getDefinition(name) as Class;
				}
				return _loadinfo.applicationDomain.getDefinition(name) as Class;
			} 
			catch (e:ReferenceError) 
			{
				return null;
			}
			return null;
		}
	}
}